package com.itcms.webservice.util;

import java.util.List;

import javax.xml.namespace.QName;

import org.apache.cxf.binding.soap.SoapHeader;
import org.apache.cxf.binding.soap.SoapMessage;
import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor;
import org.apache.cxf.headers.Header;
import org.apache.cxf.helpers.DOMUtils;
import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.phase.Phase;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.itcms.common.util.DateUtil;

public class AddSoapHeader extends AbstractSoapInterceptor {
	public static final String xml_token_el = "ESBSecurityToken";
	public static final String xml_username_el = "username";
	public static final String xml_password_el = "password";
	public static final String xml_time_el = "timestamp";
//	public static final String xml_token_el = "soap:ESBSecurityToken";
//	public static final String xml_username_el = "soap:username";
//	public static final String xml_password_el = "soap:password";
//	public static final String xml_time_el = "soap:timestamp";
	public AddSoapHeader() {
		// 指定该拦截器在哪个阶段被激发
		super(Phase.WRITE);
	}

	public void handleMessage(SoapMessage message) throws Fault {
		String username = "OutNewAdmin";
		String timestamp = DateUtil.getCurrDate(DateUtil.TIME_FORMAT);
		String password = Md5.md5Secret(timestamp);

		QName qname = new QName("ESBSecurityToken");// 这个值暂时不清楚具体做什么用，可以随便写

		Document doc = DOMUtils.createDocument();
//		Element root = doc.createElement(xml_header_el);
		Element root = doc.createElementNS("http://esb.metro.com/wsdl/soapheader/",xml_token_el);
		
		Element username_el = doc.createElement(xml_username_el);
		username_el.setTextContent(username);
		Element password_el = doc.createElement(xml_password_el);
		password_el.setTextContent(password);
		Element time_el = doc.createElement(xml_time_el);
		time_el.setTextContent(timestamp);
	
		root.appendChild(username_el);
		root.appendChild(password_el);
		root.appendChild(time_el);

		SoapHeader head = new SoapHeader(qname, root);
		List<Header> headers = message.getHeaders();
		headers.add(head);
	}
	

}
